package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.SectionParkRelationDao;
import cn.topevery.garden.entity.po.SectionParkRelation;
import cn.topevery.garden.entity.query.SectionParkRelationQuery;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.DataFillHelper;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.DbParameter;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Repository
public class SectionParkRelationDaoImpl extends BaseDaoImpl<SectionParkRelation> implements SectionParkRelationDao {

    @Override
    public  <P extends DbCsp> List<SectionParkRelation> getSearch(P para) {
        List<SectionParkRelation> res = new ArrayList<>();
        String table = "t_section_park_relation tspr";
        String columns = "tspr.*";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tspr.c_id desc");
        sqlQueryMate.where(true, "tspr.c_db_status", ScriptConditionEnum.Eq, 0);

        if(para.whereMap!=null){
            para.whereMap.forEach(f->{
                if(f.key=="sectionId"){
                    sqlQueryMate.where(true, "tspr.c_section_id", ScriptConditionEnum.Eq, f.value);
                }
            });
        }

        getDbExecute().run(sqlQueryMate.getSql(), CommandType.Text, dbCommand -> {
            for (DbParameter i : sqlQueryMate.getParameters()) {
                dbCommand.addParameter(i);
            }
            DataFillHelper.readToList(dbCommand.executeReader(), res, SectionParkRelation.class);
//            res.set(dbCommand.executeToList(SectionParkRelation.class));
        });
        return res;
    }

    

    @Override
    public DataPager<SectionParkRelation> listPage(SectionParkRelationQuery query) {
        String table = "t_section_park_relation tspr";

        String columns = "tspr.*";
		int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tspr.c_id desc");
        sqlQueryMate
                .where(true, "tspr.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(query.getSectionId() != null, "tspr.c_section_id", ScriptConditionEnum.Eq, query.getSectionId())
                .where(query.getParkId()!=null && query.getParkId()>0,"tspr.c_park_id", ScriptConditionEnum.Eq, query.getParkId())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, SectionParkRelation.class);
    }


}
